//
//  MYNewsAllRankVC.m
//  MuYing
//
//  Created by tbb on 2019/8/5.
//  Copyright © 2019 TBB. All rights reserved.
//

#import "MYNewsAllRankVC.h"
#import "YQPAdvisorySelectTypeView.h"
#import "MYShopDetailsVC.h"
#import "MYNewsRankHomeModel.h"
@interface MYNewsAllRankVC ()
@property (nonatomic, copy) NSArray * rankIconArr;
@property (nonatomic, copy) NSArray * sortTitleArr;

@property (nonatomic, strong) YQPAdvisorySelectTypeView * selctTypeView;
@property (nonatomic, copy) NSString * synthesizeSelectStr;
@end

@implementation MYNewsAllRankVC

- (void)viewDidLoad {
    [super viewDidLoad];
    //self.rankIconArr = @[@"NewsRan_result1_icon",@"NewsRan_result2_icon",@"NewsRan_result3_icon"];
    self.isNavShadow = YES;
    self.rightItemTitle = kYQLS(@"所有分类");
}

-(void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self.selctTypeView cancelSelctTableView];
}

-(void)setRankingModel:(MYNewsRankRankingModel *)rankingModel {
    _rankingModel = rankingModel;
    self.synthesizeSelectStr = rankingModel.rankingName;
    self.navigationItem.title = [NSString stringWithFormat:@"%@%@",rankingModel.rankingName,kYQLS(@"排行榜")];
    
    [self.dataArr removeAllObjects];
    [self.dataArr addObjectsFromArray:rankingModel.rankingProductList];
    
    [self.listTableView reloadData];
}

-(void)setRankHomemModel:(MYNewsRankHomeModel *)rankHomemModel {
    _rankHomemModel = rankHomemModel;
    NSMutableArray *titleArr = @[].mutableCopy;
    for (MYNewsRankRankingModel *model in rankHomemModel.rankingList) {
        [titleArr addObject:model.rankingName];
    }
    self.sortTitleArr = titleArr.copy;
    [self.listTableView reloadData];
}


-(void)setupView {
    [super setupView];
    kYQREGISTERTABLEVIEWNIBCELL(@"MYNewsRankCollViewTabelCell")
}
-(void)rightItemAction:(UIButton *)sender {
    sender.selected = !sender.selected;
    if (sender.selected) {
        [self.selctTypeView showSelctTableView];
    } else {
        [self.selctTypeView cancelSelctTableView];
    }
}
-(void)leftItemAction:(UIButton *)sneder {
    [self.selctTypeView cancelSelctTableView];
    [super leftItemAction:sneder];
}
#pragma mark - UITableViewDelegate, UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArr.count;
}

- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    MYNewsRankCollViewTabelCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MYNewsRankCollViewTabelCell"];
    
    
    MYNewsRankRankingProductModel *productModel = nil;
    if (indexPath.row < self.dataArr.count) {
        productModel = self.dataArr[indexPath.row];
    }

    cell.goodsTitleLb.text = productModel.skuName;
    cell.commentCountLb.text = [NSString stringWithFormat:@"%@%@",productModel.commentNumber,kYQLS(@"位用户评论")];
    [cell.goodsIcon sd_setImageWithURL:[NSURL URLWithString:productModel.defaultImage]];
    cell.lineView.hidden = YES;
    [cell.rankBtn setTitle:[NSString stringWithFormat:@"%zd",indexPath.row+1] forState:(UIControlStateNormal)];
    cell.starRateView.currentStarRate = productModel.score;
    
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 112;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    MYShopDetailsVC *detailsVC = [[MYShopDetailsVC alloc] init];
    MYNewsRankRankingProductModel *productModel = nil;
    if (indexPath.row < self.rankingModel.rankingProductList.count) {
        productModel = self.rankingModel.rankingProductList[indexPath.row];
    }
    detailsVC.skuId = productModel.skuId;
    [self.navigationController pushViewController:detailsVC animated:YES];
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.001;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.001;
}
#pragma mark - UITableViewDelegate, UITableViewDataSource end


#pragma mark -- lazy
-(YQPAdvisorySelectTypeView *)selctTypeView {
    if (!_selctTypeView) {
        ws(self);
        _selctTypeView = [[YQPAdvisorySelectTypeView alloc] initWithFrame:CGRectMake(0, WZTopNavH, kYQPScreenWidth, kYQPScreenHeight-WZTopNavH-kHeight_Botton_SafeArea) dataSource:self.sortTitleArr.copy selectStr:self.synthesizeSelectStr resultBlack:^(NSString * _Nonnull result ,NSInteger index) {
            self.rightItem.selected = NO;
            [weakself.selctTypeView removeFromSuperview];
            weakself.selctTypeView = nil;
            weakself.rankingModel = weakself.rankHomemModel.rankingList[index];
        } cancelBlack:^{
            self.rightItem.selected = NO;
            [weakself.selctTypeView removeFromSuperview];
            weakself.selctTypeView = nil;
        }];
    }
    return _selctTypeView;
}
#pragma mark -- lazy end
@end
